/**
 * @author roua_troua
 */
package xml;

import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;
import q10.Arbori.Nod;
import q10.Functii.FunctiiElementare.Constanta;

public class SaveXML{
	protected FileWriter fstream;
	protected BufferedWriter out;

	public SaveXML(Nod a,String fisier) throws IOException{
		fstream = new FileWriter(fisier);
		out = new BufferedWriter(fstream);
		
		out.write("<?xmlversion=\"1.0\"encoding=\"ISO-8859-1\"?>\n");
		save(a,1);
		out.close();
	}
	
	public void save(Nod a,int nivel) throws IOException{
		int i;
		
		for(i=1; i<=nivel; i++)
			out.write("");
	
		int index=(a.getClass().getName()).lastIndexOf('.');		
		String fin = a.getClass().getName().substring(index+1);
		if(fin.equals("Constanta"))
			out.write("<"+fin+"id=\""+nivel+"\"val=\""+((Constanta)a).getCt()+"\">\n");
		else
			out.write("<"+fin+"id=\""+nivel+"\">\n");
		
		if(a.getfStang() != null)
			save(a.getfStang(),nivel+1);
		if(a.getfDrept()!=null)
			save(a.getfDrept(),nivel+1);
		
		for(i=1;i<=nivel;i++)
			out.write("");
		out.write("</"+fin+">"+"\n");
	}
}
